/**
 * <p><b>Title:</b> InitTimeController.java </p>
 * @Package com.wondertek.poms.init.controller
 */
package com.wondertek.poms.init.controller;

import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.init.po.InitTime;
import com.wondertek.poms.init.service.IInitTimeService;
import com.wondertek.poms.init.vo.InitTimeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p><b>InitTimeController Description:</b> 初始化时间表Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-03-30 18:57:15</p>
 */
@Api("初始化时间")
@RestController
@Scope("prototype")
@RequestMapping(value="/initTime")
public class InitTimeController extends BaseController {
	
	@Autowired
	private IInitTimeService initTimeService;
	
	@ApiOperation("初始化时间列表")
	@GetMapping("/list")
	@ResponseBody
	public ResultBean list(HttpServletRequest request,
			InitTimeVo initTimeVo,
			@RequestParam(defaultValue="1") int page, 
			@RequestParam(defaultValue="20") int rows,
			@RequestParam(defaultValue="updatedTime") String sidx, 
			@RequestParam(defaultValue="desc") String sord) {
		InitTime initTime = new InitTime();
		BeanUtils.copyProperties(initTimeVo, initTime);
		
		Page<InitTime> pages = initTimeService
				.findAll(initTime, page, rows, sord, sidx);
		
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation("初始化时间新增")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "hour", required = true),
		@ApiImplicitParam(name = "week", required = true),
		@ApiImplicitParam(name = "mouth", required = true),
		@ApiImplicitParam(name = "year", required = true),
		@ApiImplicitParam(name = "createdBy", required = true),
		@ApiImplicitParam(name = "createdTime", required = true),
		@ApiImplicitParam(name = "updatedBy", required = true),
		@ApiImplicitParam(name = "updatedTime", required = true),
		@ApiImplicitParam(name = "remarks", required = true),
		@ApiImplicitParam(name = "del", required = true),
	})
	@PostMapping(value = "/add")
	@ResponseBody
	//TODO (需要删除不需要的属性列)
	public ResultBean add(HttpServletRequest request, InitTimeVo initTimeVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(initTimeVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(initTimeVo)) 
			return ResultBean.error(MessageCodes.get(MessageCodes.M980001));
	
		InitTime initTime = new InitTime();
		BeanUtils.copyProperties(initTimeVo, initTime);
		
		InitTime.putTimeAndUser(initTime);
		initTimeService.save(initTime);
		return ResultBean.ok(initTime);
	}
	
	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断初始化时间表名称是否重复</p>
	 * @author douzi
	 * @param initTimeVo
	 */
	private boolean isRepeat(InitTimeVo initTimeVo) {
		InitTime temp = new InitTime();
		// temp.setChannelName(initTimeVo.getName());
		Optional<InitTime> initTimeOpt = initTimeService.findOne(Example.of(temp));
		if (initTimeOpt.isPresent()) {
			InitTime initTime = initTimeOpt.get();
			if (initTimeVo.getId() != null && initTimeVo.getId() != initTime.getId())
				return true;
			else if (initTimeVo.getId() == null)
				return true;
		}
		return false;
	}
	
	@ApiOperation("初始化时间详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@ResponseBody
	public ResultBean get(Long id) {
		InitTime initTime = initTimeService.get(id);
		return ResultBean.ok(initTime);
	}
	
	@ApiOperation("初始化时间变更")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", required = true),
		@ApiImplicitParam(name = "hour", required = true),
		@ApiImplicitParam(name = "week", required = true),
		@ApiImplicitParam(name = "mouth", required = true),
		@ApiImplicitParam(name = "year", required = true),
		@ApiImplicitParam(name = "createdBy", required = true),
		@ApiImplicitParam(name = "createdTime", required = true),
		@ApiImplicitParam(name = "updatedBy", required = true),
		@ApiImplicitParam(name = "updatedTime", required = true),
		@ApiImplicitParam(name = "remarks", required = true),
		@ApiImplicitParam(name = "del", required = true),
	})
	@PutMapping(value = "/edit")
	@ResponseBody
	//TODO (需要删除不需要的属性列)
	public ResultBean update(HttpServletRequest request, InitTimeVo initTimeVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(initTimeVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(initTimeVo)) 
			return ResultBean.error(MessageCodes.get(MessageCodes.M980001));
		
		InitTime initTime = new InitTime();
		BeanUtils.copyProperties(initTimeVo, initTime);
		return ResultBean.ok(initTimeService.merge(initTime));
	}
	
	@ApiOperation("初始化时间删除")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(Long id) {
		initTimeService.deleteById(id);
		return ResultBean.ok();
	}
}
